class_name BuffUpSys
extends System

var CATCH_READ = ["FrameTime", "Ext", "SkillCmdC", "SkillLibC", "DelaySkillsC"]
var catch_read: Array

var dirty := []


func _init():
	system_name = "BuffUpSys"
	listen_create("add", "Entities", ["BuffGroupC"])
	listen_modify("modify_cast_state", "CastStateC", ["Ext", "CastStateC", "BuffGroupC"])
	listen_modify("modify_becast_state", "BeCastStateC", ["Ext", "BeCastStateC", "BuffGroupC"])


#@listenCreate(Entities)
#@read([Multi<BuffGroupC>])
func add(e, read: Array):
	read[0].insert(e.id, BuffGroupC.new())


#listenModify(CastStateC)
#read([Single<Ext>, Multi<CastStateC>, Multi<BuffGroupC>])
func modify_cast_state(e, read: Array):
	var id = e.id
	var cast = read[1].get(id) as Cast.CastStateC
	var buff_group = read[2].get(id) as BuffGroupC
	var ext = read[0].data as Ext
	if !(cast && buff_group):
		return
	if cast.action_target.action == C.ActionState.none:
		BuffUtil.instant_down(ext, id, buff_group)
	#fire buff first
	BuffUtil.fire_buff(ext, id, buff_group, cast.action_target.action)
	#add buff
	var buffs = ext.get_buffs(cast.skill, id, id, cast.action_target.action)
	if buffs:
		BuffUtil.buff_up(ext, id, buffs, buff_group)


#listenModify(BeCastStateC)
#read([Single<Ext>, Multi<BeCastStateC>, Multi<BuffGroupC>])
func modify_becast_state(e, read: Array):
	var id = e.id
	var be_cast = read[1].get(id) as Cast.BeCastStateC
	var buff_group = read[2].get(id) as BuffGroupC
	var ext = read[0].data as Ext
	if !(be_cast && buff_group):
		return
	if be_cast.action == C.ActionState.none:
		return BuffUtil.instant_down(ext, id, buff_group)
	#fire buff first
	BuffUtil.fire_buff(ext, id, buff_group, be_cast.action)
	#add buff
	var buffs = ext.get_buffs(be_cast.skill, be_cast.fighter, id, be_cast.action)
	if buffs:
		BuffUtil.buff_up(ext, id, buffs, buff_group)
